package com.caissa.framework.starter.autoconfigure.log;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.ThrowableProxy;
import ch.qos.logback.core.AppenderBase;
import com.dianping.cat.Cat;

/**
 *
 * 将所有包含异常的错误日志输出给CAT
 *
 * @auther wangwei
 * @date 2020/8/20
 */
public class CatAppender extends AppenderBase<ILoggingEvent> {

    @Override
    protected void append(ILoggingEvent event) {
        String message = event.getFormattedMessage();
        Throwable throwable = event.getThrowableProxy() != null ? ((ThrowableProxy) event.getThrowableProxy()).getThrowable() : null;

        if (throwable != null) {
            if (message != null) {
                Cat.logError(message, throwable);
            } else {
                Cat.logError(throwable);
            }
        }
    }
}
